import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName: JavaRegex <br/>
 * Function: 正则表达式学习. <br/>
 * Reason: 正则表达式. <br/>
 * date: 2017年12月15日 上午9:17:38 <br/>
 *
 * @author atc
 * @version 1.0.0
 * @since JDK 1.8
 */
public class JavaRegex {

	/**
	 * main:正则表达式. <br/>
	 * 正则表达式.<br/>
	 *
	 * @author atc
	 * @param args
	 * @since JDK 1.8
	 */
	public static void main(String[] args) {

		String str = "hello world!in java";
		Pattern pattern = Pattern.compile("w(or)(ld!)");
		Matcher matcher = pattern.matcher(str);
		
		while(matcher.find()) {
			 System.out.println("Group 0:"+matcher.group(0));//得到第0组——整个匹配  
			    System.out.println("Group 1:"+matcher.group(1));//得到第一组匹配——与(or)匹配的  
			    System.out.println("Group 2:"+matcher.group(2));//得到第二组匹配——与(ld!)匹配的，组也就是子表达式  
			    System.out.println("Start 0:"+matcher.start(0)+" End 0:"+matcher.end(0));//总匹配的索引  
			    System.out.println("Start 1:"+matcher.start(1)+" End 1:"+matcher.end(1));//第一组匹配的索引  
			    System.out.println("Start 2:"+matcher.start(2)+" End 2:"+matcher.end(2));//第二组匹配的索引  
			    System.out.println(str.substring(matcher.start(0),matcher.end(1)));//从总匹配开始索引到第1组匹配的结束索引之间子串——Wor  		
		}
		
		System.out.println("--------java正则表达式的组概念--------");
		String regex = "count(\\d+)(df)";
		String s = "count123dfwfefwefwe123";
		Pattern p = Pattern.compile(regex);
		Matcher matcher2 = p.matcher(s);
		System.out.println(matcher.groupCount());
		while (matcher2.find()) {
			System.out.println("匹配第1个括号的子串：" + matcher2.group());
		}

	}

}
